import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.security.PublicKey;
import java.text.ChoiceFormat;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.ImageIcon;
public class MakeGameToolMainClass extends Frame implements AdjustmentListener,ItemListener,ActionListener {
public Panel top,left,right,propertyPal;
public int touchX=-1,touchY=-1;
public boolean reDrawall=false;
public Scrollbar scrollAngle,scrollWidth,scrollHeight,scrollX,scrollY;
Vector<ObjectLoad> arrayLoad;
public int selectedObject=0;
public int numberOfObject=-1;
public Choice listChoose;
Image iphone;
Button writeFile;
	MakeGameToolMainClass(){
	ImageIcon ii = new ImageIcon(this.getClass().getResource("/pona.png"));
	iphone = ii.getImage();
	writeFile=new Button("Write file");
//load scroll of project
	scrollAngle=new Scrollbar(0, 1, 1, -180, 180);
	scrollWidth=new Scrollbar(0, 1, 1, 0, 320);
	scrollHeight=new Scrollbar(0, 1, 1, 0, 325);
	scrollX=new Scrollbar(0, 1, 1, 0, 320);
	scrollY=new Scrollbar(0, 1, 1, 0, 325);
//end
//loadlist;
	listChoose=new Choice();
//end
	this.setTitle("----ConverTools beta version----");
	this.setSize(750,600);
	this.setVisible(true);
	this.setLayout(null);
		top		=new Panel();
		left	=new Panel();
		right	= new Panel();
		propertyPal=new Panel();
	this.add(top);
		top.setBackground(Color.black);
		top.setBounds(25,0,550,100);
	this.add(left);
		left.setBackground(Color.red);
		left.setBounds(0,105,150,490);
	this.add(right);
		right.setBackground(Color.green);
		right.setBounds(155,105,345,490);
		arrayLoad=new Vector<ObjectLoad>();
	this.add(propertyPal);
		propertyPal.setLayout(null);
		propertyPal.setBackground(Color.GRAY);
		propertyPal.setBounds(490,105,250,490);
		propertyPal.add(listChoose);
		this.setResizable(false);
//making list
		listChoose.setBounds(20, 350, 150, 30);
		listChoose.add("---Choose your object----");
		listChoose.addItemListener(this);
//adding scroll bar and set bound
		propertyPal.add(scrollAngle);
		scrollAngle.addAdjustmentListener(this);
		scrollAngle.setBounds(13, 10, 170, 20);
		propertyPal.add(scrollWidth);
		scrollWidth.addAdjustmentListener(this);
		scrollWidth.setBounds(13, 40, 170, 20);
		propertyPal.add(scrollHeight);
		scrollHeight.addAdjustmentListener(this);
		scrollHeight.setBounds(13, 70, 170, 20);
		propertyPal.add(scrollX);
		scrollX.addAdjustmentListener(this);
		scrollX.setBounds(13, 100, 170, 20);
		propertyPal.add(scrollY);
		scrollY.addAdjustmentListener(this);
		scrollY.setBounds(13, 130, 170, 20);
				scrollAngle.setName("scrollAngle");
				scrollWidth.setName("scrollWidth");
				scrollHeight.setName("scrollHeight");
				scrollX.setName("scrollX");
				scrollY.setName("scrollY");
		writeFile.setBounds(30,400, 200, 30);
		propertyPal.add(writeFile);
		writeFile.addActionListener(this);
		//end of adding
	}
	public void Update(int choose){
		//ObjectLoad temp;
		switch (choose) {
		case 1:
			numberOfObject++;
			reDrawall=true;
			//temp=;
			arrayLoad.add(numberOfObject,new ObjectLoad(0, 0, 30, 20,0, 1));
			listChoose.add("Item"+String.valueOf(numberOfObject));
			System.out.println("add");
			break;
		case 2:
			numberOfObject++;
			//temp=new ObjectLoad(0, 0, 30,2);
			reDrawall=true;
			arrayLoad.add(numberOfObject,new ObjectLoad(0, 0, 30,2));
			listChoose.add("Item"+String.valueOf(numberOfObject));
			System.out.println("add");
			break;
		//default:
			//break;
		}
		listChoose.select(numberOfObject);
		System.out.println(arrayLoad.elementAt(0).getType());
		repaint();
	}
	public void CalculateTouch(){
		if ((touchX<=135)&&(touchX>=10)&&(touchY>=110)&&(touchY<=160))
		{System.out.println("11");
		Update(1);
		}
		Point temp;temp=new Point(touchX,touchY);
		if (temp.distance(new Point(35,(80+105)))<=70.0)
		{
		//System.out.println(Math.sqrt(Math.pow((double)(touchX-38), 2.0)+Math.pow((double)(touchY-80-105), 2.0)));
		System.out.println("22");
		Update(2);
		}
		
		
	}
		public void paint(Graphics g) {
			// TODO Auto-generated method stub
			//super.paint(g);
	// Draw main pallet
	g=left.getGraphics();
			g.setColor(Color.blue);
			g.drawString("Rectangle", 15, 15);
			g.drawRect(10,5, 135,50);
			g.drawString("Circle", 15, 80);
			g.drawArc(35, 80, 70, 70, 0, 360);
	g=right.getGraphics();
			g.setColor(Color.blue);
			g.drawRect(10,5, 320,480);//320x480 is 1/2 resolution of iphone 4
			g.fillRect(10, 5,320,55);//height of top panel in iphone is 55
			//g.drawImage(iphone, 0, 0, null);
			//the height is 480-55=325
	//draw menu of Iphone
	if(reDrawall)
		{
		g=right.getGraphics();
		g.setColor(Color.green);
		g.fillRect(0, 0,345,490);
		g.setColor(Color.blue);
		g.drawRect(10,5, 320,480);
		g.fillRect(10, 5,320,55);
		
		for (int i=1;i<=numberOfObject+1;i++)
			{
				if(arrayLoad.elementAt(i-1).getType()==1)
					{	
					g=right.getGraphics();
					g.setColor(Color.red);
					//g.fillRect(arra==yLoad.elementAt(i).getX()+10, arrayLoad.elementAt(i).getY()+65, arrayLoad.elementAt(i).getW(),arrayLoad.elementAt(i).getH());
					g.drawLine(arrayLoad.elementAt(i-1).ReturnPoint(1).x+50, arrayLoad.elementAt(i-1).ReturnPoint(1).y+100, arrayLoad.elementAt(i-1).ReturnPoint(2).x+50, arrayLoad.elementAt(i-1).ReturnPoint(2).y+100);
					g.drawLine(arrayLoad.elementAt(i-1).ReturnPoint(2).x+50, arrayLoad.elementAt(i-1).ReturnPoint(2).y+100, arrayLoad.elementAt(i-1).ReturnPoint(3).x+50, arrayLoad.elementAt(i-1).ReturnPoint(3).y+100);
					g.drawLine(arrayLoad.elementAt(i-1).ReturnPoint(3).x+50, arrayLoad.elementAt(i-1).ReturnPoint(3).y+100, arrayLoad.elementAt(i-1).ReturnPoint(4).x+50, arrayLoad.elementAt(i-1).ReturnPoint(4).y+100);
					g.drawLine(arrayLoad.elementAt(i-1).ReturnPoint(4).x+50, arrayLoad.elementAt(i-1).ReturnPoint(4).y+100, arrayLoad.elementAt(i-1).ReturnPoint(1).x+50, arrayLoad.elementAt(i-1).ReturnPoint(1).y+100);
					g.drawString("item"+String.valueOf(i-1), arrayLoad.elementAt(i-1).getX()+50, arrayLoad.elementAt(i-1).getY()+100);
					}
				else
					if(arrayLoad.elementAt(i-1).getType()==2)
						{System.out.println("my num circle "+numberOfObject);
						g=right.getGraphics();
						g.setColor(Color.red);
						g.drawOval(arrayLoad.elementAt(i-1).getX(), arrayLoad.elementAt(i-1).getY(), arrayLoad.elementAt(i-1).getR(),arrayLoad.elementAt(i-1).getR());
						g.drawString("item"+String.valueOf(i-1), arrayLoad.elementAt(i-1).getX(), arrayLoad.elementAt(i-1).getY());
						}
			}
	
		reDrawall=false;
		}
	}
		@Override
		public boolean mouseDown(Event evt, int x, int y) {
				// TODO Auto-generated method stub
			touchX=x;
			touchY=y;
			//System.out.println("this is x="+x+"this is y="+y);
			CalculateTouch();
				return true;
			}
	public static void main(String[] args) {
		new MakeGameToolMainClass();
	}
	public void updateSelectedItem()
	{
		if(arrayLoad.elementAt(selectedObject).getType()==1)
		{
			scrollAngle.setValue(arrayLoad.elementAt(selectedObject).getAlpha().intValue());
			scrollHeight.setValue(arrayLoad.elementAt(selectedObject).getH());
			scrollWidth.setValue(arrayLoad.elementAt(selectedObject).getW());
			scrollX.setValue(arrayLoad.elementAt(selectedObject).getX());
			scrollY.setValue(arrayLoad.elementAt(selectedObject).getY());
			scrollHeight.setVisible(true);
			scrollAngle.setVisible(true);
			System.out.println("----------------------------1");
		}
		else if(arrayLoad.elementAt(selectedObject).getType()==2)
		{
			scrollX.setValue(arrayLoad.elementAt(selectedObject).getX());
			scrollY.setValue(arrayLoad.elementAt(selectedObject).getY());
			scrollWidth.setValue(arrayLoad.elementAt(selectedObject).getW());
			scrollHeight.setVisible(false);
			scrollAngle.setVisible(false);
			System.out.println("----------------------------2");
		}
	}
	@Override
	public void adjustmentValueChanged(AdjustmentEvent e) {
		// TODO Auto-generated method stub
	Scrollbar scr=(Scrollbar)e.getSource();
	if (scr.getName().equals("scrollAngle")){
			arrayLoad.elementAt(selectedObject).setAlpha(scr.getValue()/1.0);
			System.out.println(selectedObject);
	}
	if (scr.getName().equals("scrollHeight")){
			arrayLoad.elementAt(selectedObject).setH(scr.getValue());
		System.out.println(scr.getValue());
	}
	if (scr.getName().equals("scrollWidth")){
		if(arrayLoad.elementAt(selectedObject).getType()==1)
			arrayLoad.elementAt(selectedObject).setW(scr.getValue());
		else {
				arrayLoad.elementAt(selectedObject).setR(scr.getValue());
			}
		System.out.println(scr.getValue());
	}
	if (scr.getName().equals("scrollX")){
		arrayLoad.elementAt(selectedObject).setX(scr.getValue());
		System.out.println(scr.getValue());
	}
	if (scr.getName().equals("scrollY")){
		arrayLoad.elementAt(selectedObject).setY(scr.getValue());
		System.out.println(scr.getValue());
	}
	reDrawall=true;
	repaint();
	//right.repaint();
	}
	@Override
	public void itemStateChanged(ItemEvent e) {
		//if (listChoose.getSelectedIndex()!=0)
		selectedObject=listChoose.getSelectedIndex()-1;
	//else selectedObject=0;
	System.out.println("----------------------------111112121:"+String.valueOf(listChoose.getSelectedIndex()));
///////
		// TODO Auto-generated method stub
		updateSelectedItem();
	////getselected item
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Button tempButton=(Button)e.getSource();
		if(tempButton.getLabel().equals("Write file")){
		try {
			Filecontroller f=(new Filecontroller());
			f.WriteOutPut();
			for (int i=0;i<=numberOfObject;i++){
				ObjectLoad temp=arrayLoad.elementAt(i);
				if(temp.getType()==1){
					f.addingLine("item"+String.valueOf(i));
					f.addingLine("Type:\t\t"+String.valueOf(temp.getType()));
					f.addingLine("X-position\t"+String.valueOf(temp.getX()));
					f.addingLine("Y-possition\t"+String.valueOf(temp.getY()));
					f.addingLine("W-lenght\t"+String.valueOf(temp.getW()));
					f.addingLine("H-lenght\t"+String.valueOf(temp.getH()));
					f.addingLine("Alpha-rotation\t"+String.valueOf(temp.getAlpha()));
					f.addingLine("item"+String.valueOf(i)+"end");
				}
				if(temp.getType()==2){
					f.addingLine("item"+String.valueOf(i));
					f.addingLine("-------------------------------------");
					f.addingLine("Type:\t\t"+String.valueOf(temp.getType()));
					f.addingLine("X-position\t"+String.valueOf(temp.getX()));	
					f.addingLine("Y-position\t"+String.valueOf(temp.getX()));
					f.addingLine("r-lenght\t"+String.valueOf(temp.getH()));
					f.addingLine("item"+String.valueOf(i)+"end");
				}
			}
			f.addingLine("-------------------------------------");
			f.close();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		}
	}
}
